﻿
using System ;

using MskEdit.Sdk ;
using MskEdit.Dxf.Representation ;


namespace MskEdit.Dxf.Model
{
    public class DxfCode
    {
#region Private data

        private static  int INVALID_CODE = -99 ;
        private         int code_ ;

#endregion

#region Private foundation

        private DxfCode( int code ) {
            code_ = code ;
        }
        
#endregion

#region Public foundation

        public static DxfCode TryFrom( string codeStr, DxfCode fallbackCode ) {
        
            int code = SdkValueParser.TryParseToInt( codeStr, INVALID_CODE ).Value ;
            
            if ( ! GroupCodeRanges.IsValidCode( code ) )
                return fallbackCode ;
                
            return new DxfCode( code ) ;
        }
        
#endregion

#region Public predicates

        public static bool IsValidCode ( string codeStr ) {
            return GroupCodeRanges.IsValidCode( SdkValueParser.TryParseToInt( codeStr, INVALID_CODE ).Value ) ;
        }

        public static bool IsStringCode ( string codeStr ) {
            return GroupCodeRanges.IsStringCode( SdkValueParser.TryParseToInt( codeStr, INVALID_CODE ).Value ) ;
        }

        public static bool IsBoolCode ( string codeStr ) {
            return GroupCodeRanges.IsBoolCode( SdkValueParser.TryParseToInt( codeStr, INVALID_CODE ).Value ) ;
        }

#endregion

#region Public properties

        public bool HasStringCode {
            get { return GroupCodeRanges.IsStringCode( code_ ) ; }
        }

        public bool HasDouble3dPointCode {
            get { return GroupCodeRanges.IsDouble3dPointCode( code_ ) ; }
        }

        public bool HasDoubleCode {
            get { return GroupCodeRanges.IsDoubleCode( code_ ) ; }
        }

        public bool HasShortCode {
            get { return GroupCodeRanges.IsShortCode( code_ ) ; }
        }

        public bool HasIntCode {
            get { return GroupCodeRanges.IsIntCode( code_ ) ; }
        }

        public bool HasBoolCode {
            get { return GroupCodeRanges.IsBoolCode( code_ ) ; }
        }

        public bool HasLongCode {
            get { return GroupCodeRanges.IsLongCode( code_ ) ; }
        }

        public int Code {
            get { return  code_ ; }
        }      

#endregion

#region Public predicates

        public bool Equals( int code ) {
            return code_ == code ;
        }

#endregion
    }
}
